﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Practices.Prism.UnityExtensions;
using Microsoft.Practices.Prism.Modularity;
using System.Collections.ObjectModel;
using Microsoft.Practices.Unity;
using HSE.Common;
using System.Linq;

namespace PetroMentor
{
    public class HSEBootstrapper : UnityBootstrapper
    {
        protected override DependencyObject CreateShell()
        {
            return Container.Resolve<HSEShell>();
        }

        //  控制页面在初始化时显示Shell页面
        protected override void InitializeShell()
        {
            base.InitializeShell();

            App.Current.RootVisual = (UIElement)this.Shell;
        }


        //  注册Module,在实际开发中可以使用xaml做配置文件，
        protected override void ConfigureModuleCatalog()
        {
            base.ConfigureModuleCatalog();

            Type banner = typeof(HSE.BannerModule);
            ModuleInfo module_View = new ModuleInfo
            {
                ModuleName = banner.Name,
                ModuleType = banner.AssemblyQualifiedName,
                InitializationMode = InitializationMode.OnDemand,
                Ref = "HSE.xap"
            };

            this.ModuleCatalog.AddModule(module_View);

            Type navi = typeof(HSE.NavigationModule);
            ModuleInfo module_navi = new ModuleInfo
            {
                ModuleName = navi.Name,
                ModuleType = navi.AssemblyQualifiedName,
                InitializationMode = InitializationMode.OnDemand,
                Ref = "HSE.xap"
            };

            this.ModuleCatalog.AddModule(module_navi);

            Type login = typeof(HSE.LoginModule);
            ModuleInfo module_login = new ModuleInfo
            {
                ModuleName = login.Name,
                ModuleType = login.AssemblyQualifiedName,
                InitializationMode = InitializationMode.WhenAvailable,
                Ref = "HSE.xap"
            };

            this.ModuleCatalog.AddModule(module_login);


            InitCatalog(SystemVar.SysInfoCollection[0].Functions);
            InitCatalog(SystemVar.SysInfoCollection[1].Functions);
        }


        //  注册服务(类)
        protected override void ConfigureContainer()
        {
            base.ConfigureContainer();
        }

        void InitCatalog(ObservableCollection<ToolBarNaviInfo> infos)
        {
            ModuleInfo modualInfo;
            foreach (ToolBarNaviInfo info in infos)
            {
                modualInfo = new ModuleInfo();
                //引用
                modualInfo.Ref = info.Ref.Trim();
                //初始化状态      
                modualInfo.InitializationMode = info.InitializationMode;
                //名称
                modualInfo.ModuleName = info.ModuleName;
                //类型
                string assemblyName = info.Ref.Trim().Replace(".xap", "");
                // modualInfo.ModuleType = assemblyName + "." + info.ModuleName + "," + assemblyName + "," + "Version=1.0.0.0, Culture=neutral,PublicKeyToken=null";
                modualInfo.ModuleType = string.Format("{0}.{1},{2},Version=1.0.0.0, Culture=neutral,PublicKeyToken=null", assemblyName, info.ModuleName, assemblyName);
                this.ModuleCatalog.AddModule(modualInfo);
                if (info.SubItems != null && info.SubItems.Count > 0)
                {
                    InitCatalog(info.SubItems);
                }
            }
        }

    }
}
